package org.example.erpapicommon.Util;

import java.util.function.Predicate;

public class PageQueryParam<T extends PageQueryParam<T>> {

    protected int pageNumber = 1;

    protected int pageSize = 10;
    public static final String ASC = "ASC";
    public static final String DESC = "DESC";

    protected String orderDirection = "ASC";

    protected String orderField;

    protected Predicate<String> checkOrderField = (orderField) -> {
        return orderField != null && !orderField.isBlank();
    };

    public PageQueryParam() {
    }

    public int getPageNumber() {
        return this.pageNumber;
    }

    public T setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
        return (T)this;
    }

    public int getPageSize() {
        return this.pageSize;
    }

    public T setPageSize(int pageSize) {
        this.pageSize = pageSize;
        return (T)this;
    }

    public T setOrderDirection(String orderDirection) {
        if (Util.equalsAnyIgnoreCase(orderDirection, new CharSequence[]{"ASC", "DESC"})) {
            this.orderDirection = orderDirection.toUpperCase();
        }

        return (T)this;
    }

    public String getOrderDirection() {
        return this.orderDirection;
    }

    public String getOrderField() {
        return this.orderField;
    }

    protected T setCheckOrderField(Predicate<String> checkOrderField) {
        if (checkOrderField != null) {
            this.checkOrderField = checkOrderField;
        }

        return (T)this;
    }

    public T setOrderField(String orderField) {
        if (this.checkOrderField != null && this.checkOrderField.test(orderField)) {
            this.orderField = orderField;
        }

        return (T)this;
    }
}
